<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠券名称">
              <a-input placeholder="请输入优惠券名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠券状态">
              <j-dict-select-tag placeholder="全部" v-model="queryParam.status" dictCode="coupon_status"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠券类型 ">
              <a-select v-model="queryParam.scopeType" @change="searchQuery" placeholder="全部">
                <a-select-option :value="1">
                  {{ shopType == 1 ? '平台通用' : '店铺通用' }}
                </a-select-option>
                <a-select-option v-if="shopType==1" :value="2">
                  指定品类
                </a-select-option>
                <a-select-option :value="3">
                  指定商品
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading"
               class="j-table-force-wrap"
               @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="minPriceSlot" slot-scope="text, record">
          满{{ record.minPrice }}&nbsp;减&nbsp;{{ record.subPrice }}
        </template>
        <template slot="receivedNumSlot" slot-scope="text, record">
          {{ record.receivedNum }}&nbsp;&nbsp;/&nbsp;&nbsp;{{ record.totalNum - record.receivedNum }}
        </template>

        <template slot="scopeType" slot-scope="text">
          <span v-if="text==1">
          <a-badge status="processing" :text="shopType == 1 ? '平台通用' : '店铺通用'"/>
          </span>
          <span v-if="text==2&&shopType==1">
            <a-badge color="yellow" text="指定品类"/>
          </span>
          <span v-if="text==3">
           <a-badge color="gold" text="指定商品"/>
          </span>
        </template>


        <template slot="statusSlot" slot-scope="text">
          <a-tag color="orange" v-if="text==1">
            未开始
          </a-tag>
          <a-tag color="green" v-if="text==2">
            进行中
          </a-tag>
          <a-tag color="red" v-if="text==3">
            已失效
          </a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.status!=3">编辑</a>

          <a-divider type="vertical" v-if="record.status!=3"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <!-- <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item> -->
              <a-menu-item>
                <a @click="detailEdit(record)">明细</a>
              </a-menu-item>
              <a-menu-item v-if="record.status!=3">
                <a @click="labelEdit(record)">发放</a>
              </a-menu-item>
              <a-menu-item v-if="record.status!=3">
                <a-popconfirm title="确定失效吗?" @confirm="() => handleDelete1(record.id)">
                  <a>失效</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.status==3">
                  <a @click="removeCoupon(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mms-coupon-modal ref="modalForm" @ok="modalFormOk"></mms-coupon-modal>
    <ums-member-ref ref="modalLabels" :selectionType="'checkbox'" @ok="modalLabelOk"></ums-member-ref>
    <mms-coupon-map-ref ref="modalDetails"></mms-coupon-map-ref>
  </a-card>
</template>

<script>
import {getAction, postAction} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import MmsCouponModal from './modules/MmsCouponModal'
import UmsMemberRef from './modules/UmsMemberRef'
import MmsCouponMapRef from './modules/MmsCouponMapRef'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'


export default {
  name: 'MmsCouponList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    MmsCouponModal,
    UmsMemberRef,
    MmsCouponMapRef
  },
  data() {
    return {
      description: '优惠券管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '优惠券id',
          align: "center",
          dataIndex: 'id'
        },
        {
          title: '优惠券名称',
          align: "center",
          dataIndex: 'name'
        },
        {
          title: '优惠券类型',
          align: "center",
          dataIndex: 'scopeType',
          scopedSlots: {customRender: 'scopeType'}
        },
        {
          title: '优惠内容',
          align: "center",
          dataIndex: 'minPrice',
          scopedSlots: {customRender: 'minPriceSlot'}
        },
        {
          title: '优惠券状态',
          align: "center",
          dataIndex: 'status',
          scopedSlots: {customRender: 'statusSlot'}
        },
        {
          title: '已领取/剩余',
          align: "center",
          dataIndex: 'receivedNum',
          scopedSlots: {customRender: 'receivedNumSlot'}
        },

        {
          title: '已使用',
          align: "center",
          dataIndex: 'useNum'
        },
        {
          title: '已过期',
          align: "center",
          dataIndex: 'expireNum'
        },

        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime'
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/mallPlus/mmsCoupon/list",
        delete: "/mallPlus/mmsCoupon/delete",
        deleteBatch: "/mallPlus/mmsCoupon/deleteBatch",
        exportXlsUrl: "/mallPlus/mmsCoupon/exportXls",
        importExcelUrl: "mallPlus/mmsCoupon/importExcel",

      },
      dictOptions: {},
      select_item: ''
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    modalLabelOk(rows, keys) {
      var that = this
      if (that.select_item) {
        postAction("/mallPlus/coupon/send", {mmsCoupon: that.select_item, memberIds: keys}).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData()
            that.onClearSelected()
          } else {
            that.$message.warning(res.message)
          }
        }).finally(() => {
          that.loading = false
        })
      }
    },
    labelEdit(record) {
      let self = this
      self.select_item = record
      self.$refs.modalLabels.visible = true
      self.$refs.modalLabels.checkedKeys = []
      self.$refs.modalLabels.checkedRows = []
      self.$refs.modalLabels.open()
    },
    detailEdit(record) {
      let self = this
      self.$refs.modalDetails.visible = true
      self.$refs.modalDetails.title = "ID：" + record.id + " 》》》" + " 名称【" + record.name + "】";
      self.$refs.modalDetails.open(record.id)
    },
    handleDelete1: function (id) {
      var that = this
      getAction("/mallPlus/mmsCoupon/invalidate", {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    removeCoupon: function (record) {
      let that = this;
      this.$confirm({
        title: "删除提示",
        content: "确认删除 ID为 " + record.id + " " + "名称为 【" + record.name + "】的优惠券吗?",
        onOk: function () {
          that.handleDelete(record.id);
        }
      });
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
